//
//  CLAlterController.m
//  CareLink
//
//  Created by clinging on 16/8/20.
//  Copyright © 2016年 Seven Team. All rights reserved.
//

#import "CLAlterController.h"
#import "SVProgressHUD.h"
#import "SSKeychain.h"
#define KUserName @"userName"
#define KUserPassWord @"passWord"

@interface CLAlterController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *oldPwd;

@property (weak, nonatomic) IBOutlet UITextField *AnewPwd;

@property (weak, nonatomic) IBOutlet UITextField *againNewPwd;

//@property (weak, nonatomic) IBOutlet UIButton *completionButton;

@end

@implementation CLAlterController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setPwd:self.oldPwd imageName:@"锁.png"];
    [self setPwd:self.AnewPwd imageName:@"锁.png"];
    [self setPwd:self.againNewPwd imageName:@"锁.png"];
    self.navigationItem.title = @"修改密码";
    //将密码输入时进行隐藏
    UITextField *test = [[UITextField alloc]init];
    test.secureTextEntry = YES;
}

//布局UI
-(void)setPwd:(UITextField *)pwd imageName:(NSString *)name

{
    //    UIImageView *imageview = [[UIImageView alloc]init];
    // 设置背景图片
    UIImage *image = [UIImage imageNamed:@"searchbar_textfield_background"];
    pwd.background = [image stretchableImageWithLeftCapWidth:image.size.width / 2 topCapHeight:image.size.height / 2];
    pwd.contentVerticalAlignment = UIControlContentHorizontalAlignmentCenter;
    //    imageview.image = image;
    //    [imageview sizeToFit];
    
    UIImageView *leftView = [[UIImageView alloc] init];
    leftView.frame = CGRectMake(0, 0, 30, 35);
    leftView.image = [UIImage imageNamed:name];
    pwd.leftView = leftView;
    
    // 设置leftViewMode
    pwd.leftViewMode = UITextFieldViewModeAlways;
    
    // 设置图片距离左边的间距，设置leftView的内容居中
    leftView.contentMode = UIViewContentModeRight;
    
}




- (IBAction)clickCompletionButton {
    
    if ([self checkTextField]) {
        // tip: 修改成功
        [SVProgressHUD showSuccessWithStatus:@"修改成功"];
        //
        
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSString *nameStr = [defaults objectForKey:KUserName];
        [SSKeychain setPassword:self.againNewPwd.text forService:[NSBundle mainBundle].bundleIdentifier account:nameStr];
        [[NSUserDefaults standardUserDefaults]setObject:self.AnewPwd.text forKey:KpassWord];
        // 返回上一个界面
        [self.navigationController popViewControllerAnimated:YES];
    }
    
}

// 文本框检测
- (BOOL)checkTextField {
    
    if (!(self.oldPwd.text.length > 0 && self.AnewPwd.text != nil && self.AnewPwd.text.length > 0 && self.AnewPwd.text != nil && self.againNewPwd.text.length > 0 && self.againNewPwd.text != nil)) {
        // tip: 文本框不能为空
        [SVProgressHUD showErrorWithStatus:@"输入的密码不能为空"];
        
        
        return NO;
    }
    
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *nameStr = [defaults objectForKey:KUserName];
    
    NSString *pwdStr = [SSKeychain passwordForService:[NSBundle mainBundle].bundleIdentifier account:nameStr];
    NSLog(@"%@",pwdStr);
    if (![pwdStr isEqualToString:self.oldPwd.text]) {
        // tip: 原密码不符
        [SVProgressHUD showErrorWithStatus:@"原密码不符"];
        
    }
    if ([self.oldPwd.text isEqualToString:self.againNewPwd.text]) {
        // tip: 两次不能相同
        [SVProgressHUD showErrorWithStatus:@"新旧密码不能相同"];
        
        return NO;
    }
    
    return YES;
}


- (IBAction)wulala:(id)sender {
    
    NSLog(@"d");
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
